/**
 * 
 */
package com.woniuxy.amusementparkos.permission.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.fzw.Power;
import com.woniuxy.amusementparkos.exception.fzw.PowerException;
import com.woniuxy.amusementparkos.permission.Service.PowerService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;



/**
 *
 * @author fzw
 *
 * 2020年4月26日下午7:39:46
 */
@Controller
public class PowerManagementController {
	//自动注入
	@Autowired
	PowerService powerService;
	
	/**
	 * 获取一页的菜单数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/powerMg/getAllPower.do")
	@ResponseBody
	public TableDataDto<Power> getAllRole(@Param("page")Integer page,@Param("limit")Integer limit){
		TableDataDto<Power> res = new TableDataDto<Power>();
		try {
			//查询一页的角色信息
			List<Power> powers=powerService.selectAllPowerByPageNumber(page,limit);
			Long count=powerService.selectCount();
			res.setCount(count);
			res.setData(powers);
			res.setCode(200);
			res.setMsg("获取权限数据成功");
			return res;
		} catch (PowerException e) {
			res.setMsg("权限处理异常，请联系管理员！");
			res.setCode(4);
			return res;
		}
		catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/powerMg/insertPower")
	@ResponseBody
	public ResponseResult<Power> insertPower(Power power){
		ResponseResult<Power> res = new ResponseResult<Power>(404,"服务器错误",null);
		try {
			//插入角色信息
			powerService.insertPower(power);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (PowerException e) {
			res.setMsg("权限添加异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/powerMg/updateOne")
	@ResponseBody
	public ResponseResult<Power> updateRole(Power power){
		ResponseResult<Power> res = new ResponseResult<Power>(404,"服务器错误",null);
		try {
			//插入角色信息
			powerService.updatePower(power);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (PowerException e) {
			res.setMsg("菜单修改异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/powerMg/deletePowers")
	@ResponseBody
	public ResponseResult<Power> deletePowers(int[] power_ids){
		ResponseResult<Power> res = new ResponseResult<Power>(404,"服务器错误",null);
		try {
			//删除菜单信息前先检验该角色是否正被用户使用，即被从表关联
			powerService.selectPowerByPowerIds(power_ids);
			//删除角色信息
			powerService.deletePowers(power_ids);
			res.setCode(200);
			res.setMsg("删除成功！");
			return res;
		}catch (PowerInUseException e) {
			res.setMsg(e.getMessage());
			res.setCode(4);
			return res;
		} 
		catch (PowerException e) {
			res.setMsg("权限删除异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	
	@RequestMapping("/powerMg/selectAllPower")
	@ResponseBody
	public ResponseResult<List<Power>> selectAllPower(){
		//初始值
		ResponseResult<List<Power>> result = new ResponseResult<List<Power>>(404,"服务器错误！",null);
		
		try {
				//查询所有菜单
			List<Power> powers=powerService.selectAllPower();
				//没有异常则设置
			result.setData(powers);
		} catch (RuntimeException e) {
			result.setCode(4);
			result.setMsg("查询失败！请联系工作人员！");
			return result;
		}catch (Exception e) {
			result.setCode(4);
			result.setMsg("系统错误！");
			return result;
		}
		result.setCode(200);
		result.setMsg("查询所有权限成功！");
		return result;
	}
}
